package com.woniuxy.shop.controller;


import com.woniuxy.shop.entity.Address;
import com.woniuxy.shop.entity.User;
import com.woniuxy.shop.service.AddressService;
import com.woniuxy.shop.service.UserService;
import com.woniuxy.shop.utils.CookieUtils;
import com.woniuxy.shop.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fanqiwen
 * @since 2023-02-07 11:56:01
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    AddressService addressService;
    @Autowired
    UserService userService;

    @RequestMapping("add")
    public String add(HttpServletRequest request,String address,String tel,String name){
        User currentUser = getCurrentUser(request);
        addressService.addAddress( address, tel, name,currentUser.getId());
        return "ok";
    }
    @RequestMapping("getAddressList")
    public List<Address> getAddressList(HttpServletRequest request){
        User currentUser = getCurrentUser(request);
        List<Address> addressList=addressService.getAddressList(currentUser.getId());
        return addressList;
    }


    private User getCurrentUser(HttpServletRequest request) {
        String token = CookieUtils.getUserToken2Cookie(request);
        String account = JwtUtils.getAccount(token);
        return userService.getUserByName(account);
    }
}

